﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Web.Mvc;
using System.Text;
using System.Reflection;
namespace System.Web.Mvc.Html
{
    public static class ListControlExtensions
    {
        /// <summary>
        /// 利用枚举生成CheckBox代码
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="className">class name</param>
        /// <param name="selectedValue">初始被选定的值</param>
        /// <param name="name">input名称</param>
        /// <returns></returns>
        public static MvcHtmlString CheckBoxFromEnum<T>(this HtmlHelper htmlHelper, int selectedValue = 0, string name = "", string className = "")
            where T : struct
        {
            var isFlag = EnumHelper.IsFlags<T>();
            var enumName = typeof(T).Name;
            var checkboxName = isFlag ? enumName : (name.IsNullOrEmpty() ? enumName + "Value" : name);

            List<STX.Domain.Forms.CheckBox> Data = EnumHelper1.GetCheckBoxData<T>(selectedValue);

            var html = new StringBuilder();
            foreach (var item in Data)
            {
                html.AppendFormat(@"<input{4} type=""checkbox"" name=""{0}"" value=""{1}""{3} />{2}",
                    checkboxName,
                    item.Value,
                    item.Text,
                    item.Checked ? " checked=\"checked\"" : "",
                    className.IsNullOrEmpty() ? "" : string.Format(" class=\"{0}\"", className));
            }

            if (isFlag)
            {
                var hideInputName = name.IsNullOrEmpty() ? enumName + "Value" : name;
                html.AppendFormat(@"<input type=""hidden"" name=""{0}"" value=""1"" />", hideInputName);

                html.AppendFormat(@"<script type=""text/javascript"">
                                        $(function () {{
                                            $(""input:checkbox[name={0}]"").click(function () {{
                                                var value = 0;
                                                $(""input:checkbox[name={0}]:checked"").each(function () {{
                                                    value += parseInt($(this).val());
                                                }});
                                                $(""input[name={1}]"").val(value);
                                            }});
                                        }});
                                    </script>", enumName, hideInputName);
            }
            return MvcHtmlString.Create(html.ToString());
        }


        public static MvcHtmlString CheckBoxList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, object htmlAttributes)
        {
            string htmlAttr = string.Empty;
            if (htmlAttributes != null)
            {
                var type = htmlAttributes.GetType();
                if (type.IsAnonymousType())
                {
                    htmlAttr = type.GetPropertiesToDict(htmlAttributes)
                                   .ToStringArray(o=>string.Format(@"{0}=""{1}""",o.Key,o.Value))
                                   .Join(" ");
                }
            }

            var sb = new StringBuilder();
            foreach (var item in selectList)
            {
                sb.AppendFormat(@"<input type=""checkbox"" name=""{0}"" value=""{1}""{3}{4}/>{2} ",
                    name,
                    item.Value,
                    item.Text,
                    item.Selected ? " checked=\"checked\"" : "",
                    htmlAttr.IsNullOrEmpty() ? "" : string.Format(" {0}", htmlAttr));
            }
            return MvcHtmlString.Create(sb.ToString());
        }


        public static MvcHtmlString DropDownListFromEnum<T>(this HtmlHelper htmlHelper, int selectedValue = 0, string name = "", string className = "")
            where T : struct
        {
            var enumName = typeof(T).Name;
            var dropDownListName = name.IsNullOrEmpty() ? enumName : name;

            var Data = EnumHelper1.GetDropDownListData<T>(selectedValue);

            var html = new StringBuilder();
            html.AppendFormat("<select name=\"{0}\"{1}>",
                dropDownListName,
                className.IsNullOrEmpty() ? "" : string.Format(" class=\"{0}\"", className));
            foreach (var item in Data)
            {
                html.AppendFormat(@"<option value=""{0}""{2}>{1}</option>",
                    item.Value,
                    item.Text,
                    item.Selected ? " selected=\"selected\"" : "");
            }
            html.Append("</select>");
            return MvcHtmlString.Create(html.ToString());
        }


        public static MvcHtmlString XDropDownList(this HtmlHelper htmlHelper, string name,IEnumerable<SelectListItem> selectList)
        {

            var html = new StringBuilder();
            html.AppendFormat("<select name=\"{0}\">",
                name);
            foreach (var item in selectList)
            {
                html.AppendFormat(@"<option value=""{0}""{2}>{1}</option>",
                    item.Value,
                    item.Text,
                    item.Selected ? " selected=\"selected\"" : "");
            }
            html.Append("</select>");
            return MvcHtmlString.Create(html.ToString());
        }

    }
}
